function ddpi_buildmex(varargin)
% Build mex files of DDP inference toolbox
%
%   ddpi_buildmex;
%       rebuilds all mex files
%
%   ddpi_buildmex name1 name2 ...
%       rebuilds those mex files listed in the arguments
%

% Created by Dahua Lin, on Nov 15, 2010
%

%% verify input

names = varargin;
if ~isempty(names)
    if ~iscellstr(names)
        error('ddpi_buildmex:invalidarg', ...
            'All inputs should be strings.');
    end
end

%% configuration

rootdir = fileparts(mfilename('fullpath'));

mlst = { ...
    {'fmm_draw_labels', {'private/fmm_draw_labels.cpp'}}, ...
    {'dpmm_seq_draw', {'private/dpmm_seq_draw.cpp'}} ...
    };

lst_names = cellfun(@(e) e{1}, mlst, 'UniformOutput', false);

if isempty(names)
    names = lst_names;
end

for i = 1 : length(names)
    
    cname = names{i};
    fprintf('building %s ...\n', cname);
    
    
    [tf, k] = ismember(cname, lst_names);
    if ~tf
        error('The target %s is not known.', cname);
    end
    
    e = mlst{k};
    srcs = cellfun(@(x) fullfile(rootdir, x), e{2}, 'UniformOutput', false);  
    outdir = fileparts(srcs{1});
              
    mex('-O', '-outdir', outdir, srcs{:});    
end

disp(' ');